#include "candlestick.h"
#include <QRectF>
#include <QPainter>
#include <QPen>


CandleStick::CandleStick(int open, int close, int high, int low)
{
    this->StickLength=high-low;
    this->RealBody = close>open?(close-open):(open-close);
    this->ShadowUp = close>open?(high-close):(high-open);
    this->ShadowLow=this->ShadowUp+this->RealBody;
    this->color=close>open?Qt::red:Qt::green;
    //setFlags(ItemIsSelectable);
    //setAcceptHoverEvents(true);
}

QRectF CandleStick::boundingRect() const
{
    return QRectF(0, 0, this->StickLength, 80);
}

QPainterPath CandleStick::shape() const
{
    QPainterPath path;
    path.addRect(15, 0, this->StickLength, 50);
    return path;
}

void CandleStick::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    QPen pen(this->color, 1);
    painter->setPen(pen);

    painter->drawRect(QRect(15, this->ShadowUp, 50,this->RealBody));

    // Draw shadowline
    painter->drawLine(40,0,40,ShadowUp);
    painter->drawLine(40,ShadowLow,40,StickLength);
}

